package org.ala.tiktools_clickhouse_generator.config;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

import org.yaml.snakeyaml.Yaml;

/**
 *
 *	@author ala
 *	@date 2025年6月9日
 */
public class ConfigInfo {
	
	
	protected DBMS dbms;
	protected Part part;
	public DBMS getDbms() {
		return dbms;
	}
	public Part getPart() {
		return part;
	}

	
	@SuppressWarnings("unchecked")
	public static ConfigInfo build() {
		Yaml yaml = new Yaml();
        Map<String, Object> root = yaml.loadAs(ConfigInfo.class.getClassLoader().getResourceAsStream("mybatis.yml"), Map.class);
        Map<String, Object> generator = (Map<String, Object>) root.get("mybatis");
        Map<String, Object> kv = (Map<String, Object>) generator.get("generator");
        
		ConfigInfo ci = new ConfigInfo();
		ci.dbms = DBMS.build((Map<String, Object>) kv.get("dbms"));
		ci.part = Part.build((Map<String, Object>) kv.get("part"));
		return ci;
	}
	
	
	/**
	 * dbms	
	 */
	public static class DBMS {
		protected String url;
		protected String username;
		protected String password;
		public static DBMS build(Map<String, Object> kv) {
			DBMS dbms = new DBMS();
			dbms.url = (String) kv.get("url");
			dbms.username = (String) kv.get("username");
			dbms.password = (String) kv.get("password");
			return dbms;
		}
		public String getUrl() {
			return url;
		}
		public void setUrl(String url) {
			this.url = url;
		}
		public String getUsername() {
			return username;
		}
		public void setUsername(String username) {
			this.username = username;
		}
		public String getPassword() {
			return password;
		}
		public void setPassword(String password) {
			this.password = password;
		}
	}
	/**
	 * 	part
	 */
	public static class Part {
		protected String[] tables;
		protected Author author;
		protected Swagger swagger;
		protected Out out;
		@SuppressWarnings("unchecked")
		public static Part build(Map<String, Object> kv) {
			Part p = new Part();
			p.tables = Arrays.stream(((String) kv.get("tables")).split(",")).map(t -> t.trim()).collect(Collectors.toList()).toArray(new String[0]);
			p.author = Author.build((Map<String, Object>) kv.get("author"));
			p.swagger = Swagger.build((Map<String, Object>) kv.get("swagger"));
			p.out = Out.build((Map<String, Object>) kv.get("out"));
			return p;
		}
		public String[] getTables() {
			return tables;
		}
		public void setTables(String[] tables) {
			this.tables = tables;
		}
		public Author getAuthor() {
			return author;
		}
		public void setAuthor(Author author) {
			this.author = author;
		}
		public Swagger getSwagger() {
			return swagger;
		}
		public void setSwagger(Swagger swagger) {
			this.swagger = swagger;
		}
		public Out getOut() {
			return out;
		}
		public void setOut(Out out) {
			this.out = out;
		}
	}
	/**
	 * 	作者
	 */
	public static class Author {
		protected String name, english;
		public static Author build(Map<String, Object> kv) {
			Author author = new Author();
			author.name = (String) kv.get("name");
			author.english = (String) kv.get("english");
			return author;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getEnglish() {
			return english;
		}
		public void setEnglish(String english) {
			this.english = english;
		}
	}
	/**
	 * 	swagger
	 */
	public static class Swagger {
		protected boolean enabled;
		public static Swagger build(Map<String, Object> kv) {
			Swagger swagger = new Swagger();
			swagger.enabled = (boolean) kv.getOrDefault("enabled", false);
			return swagger;
		}
		public boolean isEnabled() {
			return enabled;
		}
		public void setEnabled(boolean enabled) {
			this.enabled = enabled;
		}
	}
	/**
	 * 	out
	 */
	public static class Out {
		protected Entity entity;
		protected Query query;
		protected Mapper mapper;
		protected Dao dao;
		@SuppressWarnings("unchecked")
		public static Out build(Map<String, Object> kv) {
			Out out = new Out();
			out.entity = Entity.build((Map<String, Object>) kv.get("entity"));
			out.query = Query.build((Map<String, Object>) kv.get("query"));
			out.mapper = Mapper.build((Map<String, Object>) kv.get("mapper"));
			out.dao = Dao.build((Map<String, Object>) kv.get("dao"));
			return out;
		}
		public Entity getEntity() {
			return entity;
		}
		public void setEntity(Entity entity) {
			this.entity = entity;
		}
		public Query getQuery() {
			return query;
		}
		public void setQuery(Query query) {
			this.query = query;
		}
		public Mapper getMapper() {
			return mapper;
		}
		public void setMapper(Mapper mapper) {
			this.mapper = mapper;
		}
		public Dao getDao() {
			return dao;
		}
		public void setDao(Dao dao) {
			this.dao = dao;
		}
	}
	/**
	 * 	entity
	 */
	public static class Entity {
		protected String pkg;
		protected String suffix;
		public static Entity build(Map<String, Object> kv) {
			Entity e = new Entity();
			e.pkg = (String) kv.get("pkg");
			e.suffix = (String) kv.get("suffix");
			return e;
		}
		public String getPkg() {
			return pkg;
		}
		public void setPkg(String pkg) {
			this.pkg = pkg;
		}
		public String getSuffix() {
			return suffix;
		}
		public void setSuffix(String suffix) {
			this.suffix = suffix;
		}
	}
	/**
	 * 	entity
	 */
	public static class Query {
		protected String pkg;
		protected String suffix;
		public static Query build(Map<String, Object> kv) {
			Query e = new Query();
			e.pkg = (String) kv.get("pkg");
			e.suffix = (String) kv.get("suffix");
			return e;
		}
		public String getPkg() {
			return pkg;
		}
		public void setPkg(String pkg) {
			this.pkg = pkg;
		}
		public String getSuffix() {
			return suffix;
		}
		public void setSuffix(String suffix) {
			this.suffix = suffix;
		}
	}
	/**
	 * 	entity
	 */
	public static class Mapper {
		protected String pkg;
		protected String suffix;
		public static Mapper build(Map<String, Object> kv) {
			Mapper e = new Mapper();
			e.pkg = (String) kv.get("pkg");
			e.suffix = (String) kv.get("suffix");
			return e;
		}
		public String getPkg() {
			return pkg;
		}
		public void setPkg(String pkg) {
			this.pkg = pkg;
		}
		public String getSuffix() {
			return suffix;
		}
		public void setSuffix(String suffix) {
			this.suffix = suffix;
		}
	}
	/**
	 * 	entity
	 */
	public static class Dao {
		protected String pkg;
		protected String suffix;
		public static Dao build(Map<String, Object> kv) {
			Dao e = new Dao();
			e.pkg = (String) kv.get("pkg");
			e.suffix = (String) kv.get("suffix");
			return e;
		}
		public String getPkg() {
			return pkg;
		}
		public void setPkg(String pkg) {
			this.pkg = pkg;
		}
		public String getSuffix() {
			return suffix;
		}
		public void setSuffix(String suffix) {
			this.suffix = suffix;
		}
	}
}
